package org.leumasjaffe.math.probability;

import java.util.Random;

import org.eclipse.jdt.annotation.NonNull;

import flanagan.analysis.Stat;


public class WeibullRV extends AbstractRV {
	final double beta;
	final double alpha;
	
	WeibullRV(double lambda, double k) {
		this.beta = lambda;
		this.alpha = k;
	}
	
	WeibullRV(@NonNull Random r, double lambda, double k) {
		super(r);
		this.beta = lambda;
		this.alpha = k;
	}

	@Override
	public double getEV() {
		return this.beta*Stat.gamma(1+1/this.alpha);
	}

	@Override
	public double getVar() {
		return this.beta*this.beta*Stat.gamma(1+2/this.alpha) - getEV()*getEV();
	}

	@Override
	public double getP(double x) {
		return (this.alpha/this.beta)*Math.pow((x/this.beta), this.alpha-1)/
				Math.exp(Math.pow(x/this.beta,this.alpha));
	}

	@Override
	public double calcX(double p) {
		if (!isProb(p)) throw new IllegalArgumentException("Arguement is not a valid probability");
		return this.beta*Math.pow(-Math.log(1-p),1/this.alpha);
	}

	@Override
	public double getF(double x) {
		return 1 - 1/Math.exp(Math.pow(x/this.beta,this.alpha));
	}

}
